安裝 Ansible 環境
由於 Ansible 是一套開源的軟體,所以在目前大部分的主流作業系統上都已經可以透過對應的套件管理 (package manager) 進行安裝了。以下列出幾個我主要比較常用作業系統的安裝方法
安裝 Ansible 服務
macOS 安裝
以下安裝方式擇一,取決於電腦的主要安裝套件是 pip 還是 brew
sudo pip install ansible
sudo brew install ansible
CentOS 安裝
sudo yum -y install epel-release
sudo yum -y update
sudo yum -y install ansible
Ubuntu 安裝
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
Debian 安裝
sudo apt-get update
sudo apt-get install ansible
確認 Ansible 安裝完成
ansible --version
[Optional] 安裝 Ansible-lint
在編寫程式的時候,可以找對應語言的 linter 來習慣該語言的風格。雖然 Ansbile 並不是一種程式語言,但它也有一個對應的 linter - Ansible-lint
sudo pip install ansible-lint
檢查版本
ansible-lint --version
基礎設定 Ansible
藉由 ansible.cfg 設定預設的 inventory 檔案的路徑、遠端使用者名稱和 SSH 金鑰路徑
vim ansible.cfg
inventory = hosts
# 指定 inventory 檔案路徑
remote_user = username
# 遠端使用者名稱
# private_key_file = ~/.ssh/id_rsa
host_key_checking = False
# host_key_checking: 不詢問加入 ssh key
- 安裝好 Ansible 後,我們可以在
/etc/ansible/
的目錄底下找到 Ansible 的設定檔 - 通常會把
ansible.cfg
和hosts
這兩個檔案與其它的Playbooks
放在同個專案目錄底下,然後透過版本控制系統 (Git) 儲存起來 - inventory 是什麼?
- 當成是一份主機列表,定義每個 Managed Node 的代號、IP 位址、連線相關資訊和群組
- 若有對 Control Machine 本機操作的需求,建議於
/etc/ansible/hosts
補上 local 的設定
# For root user.
/bin/echo -e "[local]\nlocalhost ansible_connection=local" >> /etc/ansible/hosts
# For sudo user.
sudo su -c '/bin/echo -e "[local]\nlocalhost ansible_connection=local" >> /etc/ansible/hosts'
測試 Ansible
當已上的設置都完成了,您可以試著在終端機裡用 Ansible 呼叫本機印出 Hello World
。
ansible localhost -m command -a 'echo Hello World.'
localhost | SUCCESS | rc=0 >>
Hello World.
Ansible 的開發工具有哪些
藉 由些自動補齊 (Auto-complete)、縮排提示、語法 (Syntax) 高亮 (Highlighting) 和檢查 (Check) 等擴充套件工具來補助
純文字編輯器 (Text Editor):Vim
- ag.vim:在 Vim 裡跑 ag 的套件。
- ansible-vim:支援 Ansible 檔案 syntax highlighting 的套件。
- ctrlp.vim:模糊搜尋檔案的套件。
- dash.vim:讓 Vim 整合使用 Dash.app 的套件,但只支援 macOS。
- EnhCommentify.vim:可快速對多行註解和反註解的套件。
- indentLine:使用垂直線顯示縮排提示的套件。
- neobundle.vim:Vim 的套件管理員
- nerdtree:在 Vim 裡使用分割視窗開啟檔案管理員的套件。
- syntastic:支援多種語言的程式碼檢查套件,可用
pip
安裝 ansible-lint 來檢查 syntax 和 style - vim-gitgutter:在 Vim 裡觀看 git diff 還有新增修改等狀態的套件。
- Vim-Jinja2-Syntax:支援 Jinja2 檔案語法高亮的套件。
- YouCompleteMe:自動補齊文字的套件。
- zeavim.vim:讓 Vim 整合使用 Zeal 的套件。
Syntastic
要讓 Syntastic 可以完整支援 Ansible,還需安裝 ansible-vim 和 ansible-lint
-
使用 pip 安裝 ansible-lint。
sudo pip install ansible-lint
-
使用 NeoBundle 安裝 Syntastic 和 ansible-vim,並調整設定
vi ~/.vimrc
" ansible-vim
NeoBundle 'pearofducks/ansible-vim'
" Syntastic
NeoBundle 'scrooloose/syntastic'
let g:syntastic_enable_highlighting = 1
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*
" ignore files of Ansible Roles.
let g:syntastic_ignore_files = ['\m^roles/']
-
上面多了停止檢查
roles/
目錄的設定 -
如只想用 Syntastic 來檢查語法錯誤 (syntax error) 而不想檢查 style 的話,可把 style 關掉
let g:syntastic_quiet_messages = { "type": "style" }
Atom
Atom 是 GitHub 釋出的 open source 文字編輯器,它的自動補齊功能非常之強大,不妨可以用它來開發 Playbooks
- ansible-snippets:Snippets for Ansible.
- autocomplete-ansible:Ansible 自動補齊的套件。
- Linter-Ansible-Linting:檢查 Ansible 語法的套件,需手動使用
pip
安裝 ansible-lint。 - dash:讓 Atom 整合使用 Dash.app 的套件,但只支援 macOS。
Terminal (終 端機)
在 GNU/Linux 和 macOS 上都已內建 terminal,但較慣用以下三個 terminal emulator。
- iTerm2:macOS 上的 Terminal 不二選擇,為了要相容 Terminator 的習慣,還下了不少功夫調整。
- Terminator:GNU/Linux 上支援畫面分割的 Terminator,是個比 Gnome Terminal 順手的工具。
- rxvt-unicode:GNU/Linux 上極輕量的 Terminal。
虛擬環境 (Virtual Environment)
API 文件覽瀏器 (API Documentation Browser)
- Dash:macOS 上查詢、自動更新 API 文件的工具,其 license 為 US$ 24.99。
- Zeal:open source 版的 Dash,支援 GNU/Linux 和 Windows。
- DevDocs:網頁版的 Zeal。
- LovelyDocs:Android 上類似 Dash 的離線 API 文件瀏覽器。